package com.mayikt;

public class Test05 implements Runnable {
    static int sum;
    private SpinLock lock;

    public Test05(SpinLock lock) {
        this.lock = lock;
    }

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        SpinLock lock = new SpinLock();
        for (int i = 0; i < 100; i++) {
            Test05 test = new Test05(lock);
            Thread t = new Thread(test);
            t.start(); // 启用100个线程抢占同一个共享资源，做++操作
        }
        Thread.sleep(1000);
        System.out.println(sum);
    }

    @Override
    public void run() {
        this.lock.lock();
        System.out.println(Thread.currentThread().getName() + ": " + sum++);
        this.lock.unlock();
    }
}